from rest_framework.decorators import api_view
from rest_framework.response import Response
from rest_framework import mixins
from rest_framework import viewsets
from rest_framework import serializers
from django.http import HttpResponse
from django.contrib.auth import get_user_model
from django.views.decorators.cache import cache_page
from rest_framework_extensions.cache.mixins import CacheResponseMixin
from .models import Book
User = get_user_model()


@cache_page(10)
def cache_view(request):
    data = Book.objects.all()[0]
    user = request.user
    if request.method == 'post':
        return HttpResponse('4')
    if request.method == 'get':
        return HttpResponse('3')
    if not user:
        return HttpResponse('2')
    return HttpResponse(data.name)

class BookSerializer(serializers.ModelSerializer):
    class Meta:
        model = Book
        fields = "__all__"

class BookViewSet(CacheResponseMixin,viewsets.ReadOnlyModelViewSet):


    queryset = Book.objects.all()
    serializer_class = BookSerializer

    # def list_cache_key_func(self, **kwargs):
    #     return 'some key for list'